#include "inoise.fxh"


float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 WorldInverseTranspose;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.0;

float3 DiffuseLightDirection = float3(1, 1, 0);
float4 DiffuseColor = float4(1, 1, 1, 1);
float DiffuseIntensity = 0.7;

//float Shininess = 4;
//float4 SpecularColor = float4(1, 1, 1, 1);
//float SpecularIntensity = 0.1;
//float3 ViewVector = float3(1, 0, 0);

float4 c1 = {0.04, 0.35, 0.0, 1.0};
float4 c2 = {0.08, 0.5, 0.0, 1.0};

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float4 Normal : NORMAL0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float4 Color : COLOR0;
    //float3 Normal : TEXCOORD0;
    float3 TexCoord : TEXCOORD1;
};

float4 interpolate(float4 c1, float4 c2, float n)
{
	float4 n4 = {n,n,n,n};
	return lerp(c1, c2, n4);
}

VertexShaderOutput VS_Perlin(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

    float4 normal = normalize(mul(input.Normal, WorldInverseTranspose));
    float lightIntensity = dot(normal, DiffuseLightDirection);
    output.Color = saturate(DiffuseColor * DiffuseIntensity * lightIntensity);

    //output.Normal = normal;
    output.TexCoord = input.Position;

    return output;
}

float4 PS_Perlin(VertexShaderOutput input): COLOR
{
    //float3 light = normalize(DiffuseLightDirection);
    //float3 normal = normalize(input.Normal);
    //float3 r = normalize(2 * dot(light, normal) * normal - light);
    //float3 v = normalize(mul(normalize(ViewVector), World));
    //float dotProduct = dot(r, v);

    //float4 specular = SpecularIntensity * SpecularColor * max(pow(dotProduct, Shininess), 0) * length(input.Color);

	float noise = fBm(input.TexCoord, 1)*0.5+0.5;
	//float noise = 0.5;
	float4 color = interpolate(c1, c2, noise);
	
	//color[0] = 0.0;
	//color[1] = 0.4 + (fBm(input.TexCoord, 1)*0.5+0.5) / 3.0;
	//color[1] = 0.4 + (inoise(input.TexCoord)*0.5+0.5) / 3.0;
	//color[2] = 0.0;
	//color[3] = 1.0;
	return length(input.Color)*color;
    //return saturate(length(input.Color)*color + AmbientColor * AmbientIntensity + specular);

 }

technique fBm
{
    pass p0 
    {
		VertexShader = compile vs_3_0 VS_Perlin();
		PixelShader  = compile ps_3_0 PS_Perlin();
    }
}